The ESP32 is one of the best choices for IoT projects because of its built-in Wi-Fi, Bluetooth, and energy-saving features. In battery-powered applications, deep sleep mode is especially useful for reducing power consumption while the device waits for a trigger to wake up. The following tutorial covers how to implement deep sleep mode, discuss differences from other low-power modes like light sleep and hibernation, configure wake-up sources, and manage data across sleep cycles to help IoT devices with long runtime on a single charge.
Why Use Low Power Modes?
Low-power modes are essential for maximizing battery life in IoT projects. Here are three different modes that you might want to use depending on the needs of your project: Light Sleep, Deep Sleep, or Hibernation. Each mode offers a different power-available functionality trade-off.
ESP32 Power Consumption in Different Modes
Light Sleep Mode: During this mode, ESP32 remains connected with various peripherals ; it only stops the CPU to reduce power consumption drastically. It is best suited for short sleep cycles.
Deep Sleep Mode: In this mode, ESP32 will power almost everything down except Real Time Clock, drastically lowering power consumption. Therefore, it is perfect for a long-term sleep while retaining some of its memory.
Hibernation Mode: The lowest power consumption but no memory retention. It’s suitable for ultra-low-power applications where memory persistence is unnecessary.
Setting Up ESP32 Deep Sleep Mode
To begin, let’s look at how to configure ESP32 to enter deep sleep. The only component still turned on in this deep-sleep mode is the RTC; it keeps the internal clock running and supports wake-up events.
Example Code for Timer-Based Deep Sleep:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include "esp_sleep.h" void setup() { Serial.begin(115200); Serial.println("Entering deep sleep"); // Configure the timer to wake up the ESP32 after 10 seconds esp_sleep_enable_timer_wakeup(10 * 1000000); // Time in microseconds // Enter deep sleep esp_deep_sleep_start(); } void loop() { // No code here, as ESP32 will be in deep sleep mode } |
Figure 1: Output of Timer-Based Deep Sleep
In this example, a Timer Wake-Up puts the ESP32 into deep sleep for 10 seconds. When the timer finally expires, the device is reset.
Wake-up Sources
The ESP32 could wake up from deep sleep with several sources, as it is highly adaptable in various project needs, such as by timers, GPIOs, and touch sensors.
Wake-up Sources Overview:
Example: GPIO Wake-up:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
void setup() { Serial.begin(115200); // Configure wake-up from GPIO 33 esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0); // Wake on LOW state on pin 33 Serial.println("Going to sleep. Press button to wake up."); // Enter deep sleep esp_deep_sleep_start(); } void loop() { // Empty loop as ESP32 will be asleep } |
Figure 2: Output of GPIO Wake-up
In this case, the ESP32 wakes up when a button connected to GPIO 33 is pressed. You can also configure multiple GPIOs using the EXT1 wake-up method.
Storing Data Across Deep Sleep Cycles
When the ESP32 wakes up from deep sleep, it resets and starts executing the setup() function again. To retain data across sleep cycles, you can use RTC memory, which stays powered during deep sleep and hibernation.
Using RTC Memory:
Example: Storing Boot Count in RTC Memory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
RTC_DATA_ATTR int bootCount = 0; void setup() { Serial.begin(115200); // Increment the boot count bootCount++; Serial.println("Boot count: " + String(bootCount)); // Set ESP32 to deep sleep for 10 seconds esp_sleep_enable_timer_wakeup(10 * 1000000); Serial.println("Going to sleep..."); // Enter deep sleep esp_deep_sleep_start(); } void loop() { // No code here, ESP32 will enter deep sleep } |
Figure 3: Output of Storing Boot Count in RTC Memory
Using RTC_DATA_ATTR, variables like bootCount can persist across sleep cycles. This is useful for applications that require memory retention, such as logging data.
Best Practices for ESP32 deep sleep mode
Here are some simple tips for making the most out of deep sleep mode in your IoT projects:
Real World Applications of ESP32 deep sleep mode
This results in a deep sleep mode that is nothing less than a game-changer for applications sensitive to power efficiency. Some real-world practical examples of the use cases are listed below:
- IoT Remote Sensors: Devices periodically log environmental data and hence only have to wake up for short intervals to transmit data.
- Battery-Powered Data Loggers: Devices that record measurement of conditions, such as temperature and humidity, over time.
- Wearables: Fitness or health trackers that need to minimize power consumption while still providing functionality when required.
For example, the ESP32-based temperature and humidity logger programmed to wake up every 5 minutes and send the data to the cloud through deep sleep mode may allow it to run for extended use on batteries.
Conclusion
The low-power modes supported by ESP32 are deep sleep, light sleep, and hibernation. These three are the great methods of saving battery life for your IoT projects. You just have to choose the right mode for your needs, set up wake-up triggers, and handle data across sleep cycles. It is also worth mentioning that reducing power consumption to any particular extent basically means accepting trade-offs in the performance of the device. That makes them perfect for applications that require operating autonomously for extended periods and do so efficiently. Building IoT sensors, data loggers, or wearable tech-power consumption optimization with low-power modes will help reach your.